/**
 * 商家服务类
 * 用于获取商家列表、商家详情等商家相关操作
 */

const request = require('../libs/request');

class BizGoodsOrderService {
	

	// 获取订单详情
	static async getOrderDetail(id) {
		try {
			const response = await request.get(`/v1/biz-goods-order/detail`, {
				needAuth: true,
				data: {
					id
				}
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '获取订单详情成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '获取订单详情失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '网络请求失败'
			};
		}
	}

	static async checkTicket(code) {
		try {
			const response = await request.get(`/v1/biz-goods-order/checkTicket`, {
				needAuth: true,
				data: {
					code: code
				}
			});

			if (response.code === 1 && !response.isError) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '核销码校验成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '核销码校验失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '网络请求失败'
			};
		}
	}

	// 使用核销码
	static async useTicket(code) {
		try {
			const response = await request.post(`/v1/biz-goods-ticket/useTicket`, {
				needAuth: true,
				data: {
					code: code
				}
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '核销成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '核销失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '网络请求失败'
			};
		}
	}

	static async refundTicket(id) {
		try {
			const response = await request.post(`/v1/biz-goods-order/refundTicket`, {
				needAuth: true,
				data: {
					id: id
				}
			});

			if (response.code === 1) {
				return {
					success: true,
					data: response.data,
					message: response.msg || '退款成功'
				};
			} else {
				return {
					success: false,
					data: null,
					message: response.msg || '退款失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				data: null,
				message: error.message || '网络请求失败'
			};
		}
	}

	static async complaint(orderId, content) {
		try {
			const response = await request.post(`/v1/biz-goods-order/complaint`, {
				order_id: orderId,
				content: content,
				type:'biz-order'
			},{
				needAuth: true,
			});

			if (response.code === 1) {
				return {
					success: true,
					message: response.msg || '投诉提交成功'
				};
			} else {
				return {
					success: false,
					message: response.msg || '投诉提交失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				message: error.message || '网络请求失败'
			};
		}
	}

	static async getOrderListByBiz( page = 1, limit = 10,keyword = '') {
		const response = await request.get(`/v1/biz-goods-order/get-order-list-by-biz`, {
			needAuth: true,
			data: { 
				page: page,
				limit: limit,
				keyword:keyword
			}
		});

		return {
			success: response.code === 1,
			data: response.data,
			message: response.msg || '获取消费订单列表成功'
		};
	}

	static async getTicketListByBiz(page = 1, limit = 10) {
		const response = await request.get(`/v1/biz-goods-ticket/get-ticket-list-by-biz`, {
			needAuth: true,
			data: { page: page, limit: limit }
		});

		return {
			success: response.code === 1,
			data: response.data,
			message: response.msg || '获取核销码列表成功'
		};
	}

	static async doHide(id) {
		try {
			const response = await request.post(`/v1/biz-goods-order/do-hide`, {
				id: id
			},{
				needAuth: true,
			});

			if (response.code === 1) {
				return {
					success: true,
					message: response.msg || '隐藏成功'
				};
			}
			else {
				return {
					success: false,
					message: response.msg || '隐藏失败'
				};
			}
		} catch (error) {
			return {
				success: false,
				message: error.message || '网络请求失败'
			};
		}
	}

	
}

module.exports = BizGoodsOrderService;
